#include "t0022-0.h"
#include "dxtviewer.h"
#include "appwidget.h"
#include "cui.h"
#include <signal.h>
#include "cui_messagebox.h"

#define MODNAME     "[MBX]"
extern struct dxtviewer_st dv;
int messagebox_keyevent(struct cui_widget_st* widget, int key)
{
	int handled = CUI_WIDGETEVENT_PROPAGATION;
    struct cui_messagebox_st *cmsgbox = widget->priv;

	ERRSYS_INFOPRINT("%s:%s\n", widget->name, __func__);
	switch (key) {
        case KEY_LEFT:
            cmsgbox->choice --;
            if (cmsgbox->choice < 0) {
                cmsgbox->choice = 0;
            }
            handled = CUI_WIDGETEVENT_HANDLED;
            break;
        case KEY_RIGHT:
            cmsgbox->choice ++;
            if (cmsgbox->choice >= cmsgbox->nchoices) {
                cmsgbox->choice = cmsgbox->nchoices - 1;
            }
            handled = CUI_WIDGETEVENT_HANDLED;
            break;
		case KEY_ENTER_US:
		case KEY_ENTER:
		case KEY_ESC:
            key = KEY_ENTER;
			handled = CUI_WIDGETEVENT_HANDLED;
			break;
	}

    if (KEY_ENTER == key) {
        cui_screen_pop(widget->cscr);
        cui_refresh(widget->cscr, CUI_CURSOR_INVISIBLE);
        ERRSYS_INFOPRINT("messagebox quit\n");
    }
	return handled;
}

int messagebox(uint32_t msglevel, uint32_t buttons, const char* stream, ...)
{
    int retval = -1;
    struct cui_widget_st *widget = get_widget_byname(&dv.cscr->windowlist, "messagebox");

    if (widget) {
        struct cui_messagebox_st *cmsgbox = widget->priv;
        struct cui_menu_st *cmenu;
        char *button_texts[3] = {NULL, NULL, NULL};
        int nbuttons = 0;
        int textrows = 0;
        int menurows = 0;   // widget menu rows
        int menucols = 0;   // widget menu cols
        int rows = 0;       // parent widget rows

        va_list arg_ptr;
        va_start(arg_ptr, stream);

        if (msglevel == MESSAGEBOX_MSGLEVEL_ERROR) {
            cmsgbox->msglevel = msglevel;
            snprintf(widget->title, CUI_WIDGETTITLE_BUFLEN, "Error");
        }
        else if (msglevel == MESSAGEBOX_MSGLEVEL_FATAL) {
            snprintf(widget->title, CUI_WIDGETTITLE_BUFLEN, "Fatal");
        }
        else {
            snprintf(widget->title, CUI_WIDGETTITLE_BUFLEN, "Information");
        }
        vsnprintf(cmsgbox->message, CUI_MESSAGEBOX_BUF_LEN, stream, arg_ptr);
        rows = CUI_WIDGET_TITLESPLITTER_WIDTH + CUI_WIDGET_TITLE_HEIGHT + CUI_WIDGET_BORDER_WIDTHX2;

        va_end(arg_ptr);

        if (buttons == MESSAGEBOX_BUTTON_OK) {
            button_texts[0] = "OK";
            menucols = strlen(button_texts[0]);
            menurows = 1;
            nbuttons = 1;
        }
        else if (buttons == MESSAGEBOX_BUTTON_CANCEL) {
            button_texts[0] = "Cancel";
            menucols = strlen(button_texts[0]);
            menurows = 1;
            nbuttons = 1;
        }
        else if (buttons == MESSAGEBOX_BUTTON_YES) {
            button_texts[0] = "Yes";
            menucols = strlen(button_texts[0]);
            menurows = 1;
            nbuttons = 1;
        }
        else if (buttons == MESSAGEBOX_BUTTON_NO) {
            button_texts[0] = "No";
            menucols = strlen(button_texts[0]);
            menurows = 1;
            nbuttons = 1;
        }
        else if (buttons == MESSAGEBOX_BUTTON_OKCANCEL) {
            button_texts[0] = "OK";
            button_texts[1] = "Cancel";
            menucols = strlen(button_texts[1]) * 2 + 2;
            menurows = 1;
            nbuttons = 2;
        }
        else if (buttons == MESSAGEBOX_BUTTON_YESNO) {
            button_texts[0] = "Yes";
            button_texts[1] = "No";
            menucols = strlen(button_texts[0]) * 2 + 2;
            menurows = 1;
            nbuttons = 2;
        }
        else {
            button_texts[0] = "OK";
            menucols = strlen(button_texts[0]);
            menurows = 1;
            nbuttons = 1;
        }
        cmsgbox->buttons = buttons;
        cmsgbox->nchoices = nbuttons;

        rows += menurows + CUI_WIDGET_BORDER_MARGIN;

        cui_menu_initialize(widget, nbuttons, (const char**)button_texts, NULL, -1);
        // clear menu mark
        cui_menu_mark(widget, "");
        textrows = cui_autoreturn_rows(widget, 0, cmsgbox->message);
        rows += textrows + CUI_WIDGET_BORDER_MARGIN;

        cui_widget_resize(widget, rows, -1, -1, -1);
        cui_widget_align(widget, CUI_WIDGET_ALIGNMENT_XYMID);

        cmenu = widget->widgetinstance;
        if (cmenu) {
            cui_menu_resize(widget, menurows, menucols, cmenu->beginy + textrows + 1, -1, 1, 2);
            cui_menu_align(widget, CUI_WIDGET_ALIGNMENT_XMID);
        }
        cui_dump_menu(widget);

		cui_screen_push(widget, NULL);
		cui_refresh(widget->cscr, -1);
    }

    return retval;
}

int messagebox_paint(struct cui_widget_st* widget, int dummy)
{
    struct cui_messagebox_st *cmsgbox;
	ERRSYS_INFOPRINT("%s %s\n", __func__, widget->name);
    if (widget) {
        cmsgbox = widget->priv;
        if (cmsgbox) {
            cui_print_autoreturn(widget, 2, 0, cmsgbox->message, WHTBLK);
        }
    }

	return 0;
}

int messagebox_initialize(struct cui_widget_st* widget)
{
    int retval = -1;
    ERRSYS_INFOPRINT("%s\n", __func__);
    widget->priv = zmalloc(sizeof(struct cui_messagebox_st));
    if (widget->priv) {
        cui_set_widget_eventslot(widget, CUI_EVENTCB_TYPE_PAINT, messagebox_paint);
        retval = 0;
    }
    return retval;
}

EXPORT_WIDGET_INITIALIZE(messagebox_initialize);